import { http, fileUploadHttp } from "../http";

const baseUrl = "/api/dms/files";

// 批量获取文件详情请求参数
export interface BatchFileDetailsRequest {
  ids: string; // 文件ID数组
}

/**
 * @description 删除文件
 * @param id 文件ID
 */
export const deleteFileAPI = (id: string) => {
  return http.delete(`${baseUrl}/${id}`);
};

/**
 * @description 批量获取文件详情
 * @param params 包含文件ID数组的请求参数
 */
export const getBatchFileDetailsAPI = (params: BatchFileDetailsRequest) => {
  return http.post(`${baseUrl}/details`, params);
};

/**
 * @description 文件上传
 * @param file 要上传的文件对象
 * @param onProgress 上传进度回调函数（可选）
 */
export const uploadFileAPI = (
  file: File,
  onProgress?: (progress: number) => void
) => {
  const formData = new FormData();
  formData.append("file", file);

  // 使用专门的文件上传http实例
  return fileUploadHttp.post(`${baseUrl}/upload`, formData, {
    onUploadProgress: (progressEvent) => {
      if (onProgress && progressEvent.total) {
        const progress = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
        onProgress(progress);
      }
    },
  });
};
